class Solution
{
public:
    vector<vector<int>> differenceOfDistinctValues(vector<vector<int>> &grid)
    {
        int m = grid.size();
        int n = grid[0].size();

        vector<vector<int>> result(m, vector<int>(n, 0));

        for (int row = 0; row < m; ++row)
        {
            for (int col = 0; col < n; ++col)
            {
                unordered_set<int> topLeft;
                unordered_set<int> bottomRight;

                {
                    int i = row - 1;
                    int j = col - 1;
                    while (i >= 0 && j >= 0)
                    {
                        topLeft.insert(grid[i][j]);
                        --i;
                        --j;
                    }
                }

                {
                    int i = row + 1;
                    int j = col + 1;
                    while (i < m && j < n)
                    {
                        bottomRight.insert(grid[i][j]);
                        ++i;
                        ++j;
                    }
                }

                result[row][col] = labs(topLeft.size() - bottomRight.size());
            }
        }
        return result;
    }
};